package com.inight.nightojbackendgateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsConfiguration;

import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

import java.util.Arrays;

/**
 * 统一跨域配置
 */
@Configuration
public class CrosConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 允许所有方法和添加请求头
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        // 表示允许跨域
        config.setAllowCredentials(true);
        // todo 允许跨域的地址. 实际改为线上真实域名、本地域名
//        config.setAllowedOrigin("*"); 不建议这样设置, 会出错, 前端也会报错
        config.setAllowedOriginPatterns(Arrays.asList("127.0.0.1"));
        // todo 注意包是来自 reactive 的
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);

    }
}



